from quantification.strategy.indicator.Indicator import IndicatorHandler


class VolumeIndicators(IndicatorHandler):
    '''
    成交量指标
    '''

    def __init__(self, indicatorHandler: IndicatorHandler = None):
        self.next = indicatorHandler

    def handle(self, pd_data):
        '''
        成交量处理（放量，缩量，平量，地量，巨量）
        :param pd_data:
        :return:
        '''
        self.volume_analysis(pd_data)
        if self.next:
            self.next.handle(pd_data)

    def volume_analysis(self, pd_data, period=30):
        df_last = pd_data[['close', 'volume']]
        df_last['ranking'] = df_last['volume'].rolling(period).rank()
        df_last['score'] = df_last['ranking'].map(lambda x: 2 * (x - 1) / period - 1)
        # df_last['2022-09']
        df_last.loc[df_last['score'] > 0.8, 'volumn_signal'] = '放量'
        pd_data['volumn_signal'] = df_last['volumn_signal']
